package core.game;

import core.state.BasicState;

import recorder.input.InputManagerRecorder;

import java.awt.Graphics2D;
import java.util.ArrayList;


public abstract class StateBasedGame extends Game{

	private ArrayList<BasicState> listState = new ArrayList<BasicState>();
	private BasicState currentState;
	
	public StateBasedGame(String name) {
		super(name);
	}
	
	public abstract void initStates();
	
	public abstract void updateStates();
	
	protected void addState(BasicState state){
		listState.add(state);
	}
	
	public void enterState(int num){
		if(currentState != null){
			inputManager.removeInputReceiver((InputManagerRecorder)currentState.getInputManager());
		}
		currentState = null;
		for(int i=0; i<listState.size(); i++){
			if(listState.get(i).getID() == num){
				currentState = listState.get(i);
				currentState.enter();
				inputManager.addInputReceiver((InputManagerRecorder)currentState.getInputManager());
				break;
			}
		}
	}
	
	public BasicState getState(int num){
		BasicState bs = null;
		for(int i=0; i<listState.size(); i++){
			if(listState.get(i).getID() == num){
				bs = listState.get(i);
				break;
			}
		}
		return bs;
	}

	@Override
	public final void init() {
		initStates();
	}
	
	@Override
	public final void render(Graphics2D g) {
		if(currentState != null){
			currentState.render(g);
		}
	}

	@Override
	public final void update(int deltaTime) {
		if(currentState != null){
			currentState.update(deltaTime);
		}
	}

}
